import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.concurrent.TimeUnit;

/**
 * Created with IDEA
 * Author : wangfei
 * Date :  2016/7/21
 * Time : 17:47
 * Describe :
 */
public class JmsReceiver {
	public static  void main(String[] args){
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");


		try {
			Connection connection = connectionFactory.createConnection();
			connection.start();

			final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue("Test.foo");

			MessageConsumer consumer = session.createConsumer(destination);
			//listener 方式
			consumer.setMessageListener(new MessageListener() {

				public void onMessage(Message msg) {
					ObjectMessage message = (ObjectMessage) msg;
					//TODO something....
					try {
						User user = (User) message.getObject();
						System.out.println("收到消息："+user.toString());
					} catch (JMSException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
						session.commit();
					} catch (JMSException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			});
			TimeUnit.MINUTES.sleep(1);

			session.close();
			connection.close();
		} catch (JMSException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}
